package org.rency.crawler.fetch.handler.impl;

import javax.annotation.Resource;
import org.rency.crawler.common.exception.CrawlerException;
import org.rency.crawler.domain.state.FetchStatus;
import org.rency.crawler.fetch.handler.AbstractFetchHandler;
import org.rency.crawler.fetch.helper.FetchCarrier;
import org.rency.crawler.fetch.repository.FetchedQueueRepository;
import org.springframework.stereotype.Component;

/**
 * 名称:FetchAfterHandler
 * 描述:${TODO}
 *
 * @author:rency
 * @version:V1.0.0
 * @JDK 1.8
 * @since 2017/11/22 22:22
 */
@Component
public class FetchAfterHandler extends AbstractFetchHandler{

  @Resource
  private FetchedQueueRepository fetchedQueueRepository;

  @Override
  protected String getHandlerName() {
    return "抓取后置";
  }

  @Override
  protected void onHandler(FetchCarrier carrier) throws CrawlerException {
    logger.debug("抓取{},流经状态:{}.",carrier.getLogPrefix(),carrier.getFetchStateHistory());
    carrier.getFetchState().setFsStatus(FetchStatus.SUCCESS);
    if(!carrier.getResponse().isSuccess()){
      //抓取失败,则从已抓取队列中移除
      fetchedQueueRepository.delete(carrier.getRequest().getFetchNo());
    }
  }

}
